﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.Common;

namespace Web.Cms.AjaxCP
{
    public partial class PagingAjax : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }       
        public int totalRecords;
        public int numPerPage;
        public string defaultQueryParam;
        public string queryParam;
        public int numPages = 10;
        public int cPage = 1;
        public string callback = "GetCommentsAjax";
        public string url = "/Home/AjaxResponse/GetList.aspx";
        public int _flag;
        protected override void OnPreRender(EventArgs e)
        {
            if (Request.QueryString.Get("cPage") != null)
                cPage = ObjConvertUtility.ToInt32(Request.QueryString.Get("cPage"));
            int totalPages = (int)Math.Ceiling((double)totalRecords / numPerPage);
            int c = (int)Math.Ceiling((double)cPage / numPages);
            int from = (c - 1) * numPages + 1;
            int to = c * numPages;
            if (to > totalPages) to = totalPages;
            ArrayList x = new ArrayList();
            for (int i = from; i <= to; i++) x.Add(i);
            rptPaging.DataSource = x;
            if (totalPages == 1)
            {
                lnkNext.Visible = false;
                lnkPrev.Visible = false;
                lnkFirst.Visible = false;
                lnkLast.Visible = false;
                _flag = 1;
            }
            if (cPage > 1)
            {
                lnkPrev.Attributes.Add("onclick", callback + "('" + url + queryParam + (cPage - 1) + "')");
            }
            if (cPage < totalPages)
            {
                lnkNext.Attributes.Add("onclick", callback + "('" + url + queryParam + (cPage + 1) + "')");
            }

            rptPaging.ItemDataBound += new RepeaterItemEventHandler(rptPaging_ItemDataBound);
            rptPaging.DataBind();
            lnkFirst.Attributes.Add("onclick", callback + "('" + url + queryParam + "1" + "')");
            lnkLast.Attributes.Add("onclick", callback + "('" + url + queryParam + totalPages + "')");

            int _from = (cPage - 1) * numPerPage + 1;
            int _to = cPage * numPerPage;
            if (totalRecords == 0)
                lblFrom.Text = "0";
            else
                lblFrom.Text = _from.ToString();
            if (_to > totalRecords)
                lblTo.Text = totalRecords.ToString();
            else
                lblTo.Text = _to.ToString();
            lblTotal.Text = totalRecords.ToString();
            if (totalRecords == 0) this.Visible = false;
        }

        public void rptPaging_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HyperLink lnkPageBreak = (HyperLink)e.Item.FindControl("lnkPageBreak");
            lnkPageBreak.Attributes.Add("onclick", callback + "('" + url + queryParam + e.Item.DataItem + "')");
            lnkPageBreak.Text = ObjConvertUtility.ToString(e.Item.DataItem);
            if (cPage == ObjConvertUtility.ToInt32(lnkPageBreak.Text))
            {
                lnkPageBreak.BackColor = Color.FromName("#C0C0C0");
                lnkPageBreak.Font.Bold = true;
                lnkPageBreak.ForeColor = Color.Black;
            }
            if (_flag == 1)
            {
                lnkPageBreak.Visible = false;
            }
        }
    }
}